package pl.edu.agh.dsrg.sr.chat.consoleClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import pl.edu.agh.dsrg.sr.chat.common.ConfigReader;
import pl.edu.agh.dsrg.sr.chat.common.ManagementChannelNotificationAppender;
import pl.edu.agh.dsrg.sr.chat.common.MessageChanelNotificationAppender;
import pl.edu.agh.dsrg.sr.chat.communicator.ChatCommunicator;

public class MainConsole {
	private ChatCommunicator client;
	private ManagementChannelNotificationAppender managementAppender;
	private MessageChanelNotificationAppender messageAppender;

	// java -Djgroups.bind_addr=127.0.0.1 -Djava.net.preferIPv4Stack=true
	public static void main(String[] args) {
		MainConsole main = new MainConsole();
		try {
			main.init();
			main.eventLoop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void init() throws Exception {
		String nick = readNick();

		managementAppender = new ConsoleManagementNotificationAppender();
		messageAppender = new ConsoleMessageChannelAdapter();

		client = new ChatCommunicator(nick,
				(ConfigReader.getManagementChanelName()), managementAppender,
				messageAppender);
		client.start();
	}

	private String readNick() {
		System.out.println("Typeyour nick:");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = ConfigReader.getNick();
		try {
			line = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return line;
		// return ConfigReader.getNick();
	}

	private void eventLoop() throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Welcome in chat group, " + client.getNick());
		while (true) {
			System.out.print("<<< ");
			System.out.flush();
			String line = in.readLine();
			if (line == null) {
				continue;
			}

			switch (line) {
			case "": {
				break;
			}
			case "help": {
				System.out
						.println("###### info, exit, create, join, list, leave, help");
				break;
			}
			case "info": {
				String currentChannel;
				try {
					currentChannel = client.getActiveSendingChannelName();
				} catch (NullPointerException e) {
					currentChannel = "NONE";
				}

				System.out.println("###### nick: " + client.getNick()
						+ ", active sending channel: " + currentChannel);
				break;
			}
			case "exit": {
				client.stop();
				System.exit(0);
				break;
			}
			case "create": {
				String channelName = readProperMultiIPAddr();
				client.createChannel(channelName);
				break;
			}
			case "join": {
				String channelName = readProperMultiIPAddr();
				client.joinChannel(channelName);
				break;
			}
			case "list": {
				printChannelsAndUsers();
				break;
			}
			case "leave": {
				String channelName = readProperMultiIPAddr();
				client.leaveMessageChannel(channelName);
				break;
			}
			default: {
				client.sendMessage(line);
			}

			}
		}
	}

	private String readProperMultiIPAddr() {
		System.out.println("Type valid multicast IP (230.0.0.36):");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "230.0.0.36";
		try {
			line = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return line;
	}

	private void printChannelsAndUsers() {
		System.out.println(client.getChatState());
	}

}
